/*0GI1xcoeligdpMeXoHBphhVCrhxcviGrDnphTJgmfcwMg4RNPBhEzXRaezj70QBs*/
import { ajax } from 'nc-lightapp-front';
import constants from '../../constants';
import { linkToCard, deleteLine } from './buttonClick';
import ampub from 'ampub';
import fa from 'fa';

const { utils: { msgUtils: { showMessage }, multiLangUtils: { getMultiLangByID } } } = ampub;
const { fa_components: { CardLinkToBillUtil: { CardLinkToBill } } } = fa;

const { BILL_SOURCE_TYPES, LIST_BTNS: { TABLE_BODY }, FIELDS: { HEAD_PK: headPk, BODY_PK: bodyPk } } = constants;

export default function(props, key, text, record, index) {
	switch (key) {
		case TABLE_BODY.EDIT:
			edit(props, record);
			break;

		case TABLE_BODY.COPY:
			doCopyOnTable(this, record);
			break;

		case TABLE_BODY.DELETE:
			deleteLine(props, { values: record });
			break;

		case TABLE_BODY.QueryAboutBusiness:
			billreview(props, record);
			break;

		default:
			break;
	}
}

function edit(props, record) {
	const pk_card = record[headPk].value;
	const promise = new Promise(function(resolve, reject) {
		if (pk_card) {
			ajax({
				url: constants.URL.EDIT,
				data: {
					pk: pk_card,
					resourceCode: '2012016030' //固定资产卡片维护和原始卡片走同一个资源号
				},
				success: (res) => {
					let { success, data } = res;
					if (success) {
						let isValidate = data['validate'];
						let failure = data['failure'];

						if (failure) {
							reject(failure);
							return;
						}
						if (!isValidate) {
							reject(isValidate);
							return;
						}
						resolve(data);
					}
				},
				error: (err) => {
					reject(err);
				}
			});
		}
	}).then(
		(resData) => {
			//卡片校验通过，可以编辑
			linkToCardOnRow(props, record, constants.UISTATE.EDIT);
		},
		(err) => {
			showMessage(props, { content: err.message || err || getMultiLangByID('201201504A-000013'), color: 'danger' });
		}
	);
}

//table 行点击的跳转到卡片态
function linkToCardOnRow(props, record, status) {
	const id = record[bodyPk].value;
	const param = {
		status,
		id,
		transiType: record.transi_type.value,
		pagecode: record.transi_type.value
	};
	linkToCard(props, param);
}

//表格操作列 copy
function doCopyOnTable(that, record) {
	const pk_card = record.pk_card.value;
	that.facardCopy.showCopyCardDialog(that, pk_card);
}

//单据追溯
function billreview(props, record) {
	let {
		// 来源单据类型
		bill_source: { value: bill_source },
		// 来源单据交易类型
		transi_type_source: { value: transi_type_source },
		// 来源单据pk
		pk_bill_src: { value: pk_src }
	} = record;

	//卡片来源交易类型 有默认值handin
	if (transi_type_source === BILL_SOURCE_TYPES.handin_src) {
		transi_type_source = null;
	}

    let type_src = transi_type_source || bill_source;

    if (bill_source !== BILL_SOURCE_TYPES.handin_src) {
		if (pk_src && type_src && !type_src.startsWith(BILL_SOURCE_TYPES.equip_src)) {
			CardLinkToBill.call(this, props, pk_src, type_src);
		} else {
			/*国际化处理：该单据的来源单据号错误*/
			showMessage(props, { content: getMultiLangByID('201201504A-000021'), color: 'warning' });
		}
	} else {
		/*国际化处理：该单据没有来源单据*/
		showMessage(props, { content: getMultiLangByID('201201504A-000022'), color: 'warning' });
	}
}

export { linkToCardOnRow };

/*0GI1xcoeligdpMeXoHBphhVCrhxcviGrDnphTJgmfcwMg4RNPBhEzXRaezj70QBs*/